//
//  PictureViewController.m
//  SkillzOn
//
//  Created by iD Student on 7/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PictureViewController.h"

#import <QuartzCore/QuartzCore.h>



@interface PictureViewController ()

@end

@implementation PictureViewController
@synthesize imageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) useCamera
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = 
        UIImagePickerControllerSourceTypeCamera;    
//        imagePicker.mediaTypes = [NSArray arrayWithObjects:
//                                  (NSString *) kUTTypeImage,
//                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentModalViewController:imagePicker animated:YES];
        newMedia = YES;
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //UIImageView * touchDraw = imageView;
    UIImage * textureColor = [UIImage imageNamed:@"brushImg.png"];
    NSArray *clicks=[touches allObjects];
    UIGraphicsBeginImageContext(imageView.bounds.size);
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    for (int i=0; i<[clicks count]; i++) {
        UITouch *touch = (UITouch *)[clicks objectAtIndex:i];
        currentPoint = [touch locationInView:self.view];
        UIImageView *currentBrush=[[UIImageView alloc]initWithImage:textureColor];
        currentBrush.center=currentPoint;
        [imageView addSubview:currentBrush];
        [textureColor drawInRect:CGRectMake(currentPoint.x, currentPoint.y, textureColor.size.width, textureColor.size.height)];
        NSLog(@"%f - %f",currentPoint.x,currentPoint.y);
        
    
    //currentPoint.y -= 685;     //<
    //currentPoint.x = ???;  <------ adjust the x and y points of the currentPoint if it is not on the exact point of drawing.
    
//    UIGraphicsBeginImageContext(touchDraw.frame.size);
//    [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)];
//    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
////    UIImage * textureColor = [UIImage imageNamed:@"brushImg.png"];
//    
//    CGPoint vector = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
//    CGFloat distance = hypotf(vector.x, vector.y);
//    vector.x /= distance;
//    vector.y /= distance;
//    for (CGFloat i = 0; i < distance; i += 1.0f) {
//        CGPoint p = CGPointMake(300,300);
//        [textureColor drawAtPoint:p blendMode: kCGBlendModeNormal   alpha:1];
//    }
//    CGContextBeginPath(UIGraphicsGetCurrentContext());
//    CGContextStrokePath(UIGraphicsGetCurrentContext());
//    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    previousPoint = currentPoint;
    }
    
    viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
}
- (void) useCameraRoll
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = 
        UIImagePickerControllerSourceTypePhotoLibrary;
//        imagePicker.mediaTypes = [NSArray arrayWithObjects:
//                                  (NSString *) kUTTypeImage,
//                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentModalViewController:imagePicker animated:YES];
        newMedia = NO;
    }
}

- (IBAction)SaveButton:(id)sender {
    
    
    UIImageWriteToSavedPhotosAlbum(viewImage, 
                                   self,
                                   @selector(image:finishedSavingWithError:contextInfo:),
                                   nil);
}



-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    [self dismissModalViewControllerAnimated:YES];
    
    
    imageView.image = image;
    
    
    
}


-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error 
 contextInfo:(void *)contextInfo
{
    NSLog(@"in_SAVE");
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Save failed"
                              message: @"Failed to save image"\
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
 
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.imageView = nil;
}


@end
